﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace FTC.SharePoint.Evolution.Core
{
    enum SPEvolutionLogType
    {
        Console,
        SharePointLog,
        File
    }
    enum SPEvolutionLogLevel
    {
        Debug,
        Exception
    }
    class SPEvolutionLog // fixed slngleton bug for sharepoint
    {

        private static SPEvolutionLog _Instance = null;
        private static readonly object _Lock = new object();

        private SPEvolutionLog()
        {
            LogTypes = new List<SPEvolutionLogType>();
        }

        public static SPEvolutionLog Instance
        {
            get
            {
                lock (_Lock)
                {
                    if (_Instance == null)
                        _Instance = new SPEvolutionLog();

                    return _Instance;
                }
            }
        }
      
        //private sealed class SingletonCreator
        //{
        //    private static readonly SPEvolutionLog instance = new SPEvolutionLog();
        //    public static SPEvolutionLog Instance { get { return instance; } }
        //}

        //public static SPEvolutionLog Instance
        //{
        //    get { return SingletonCreator.Instance; }
        //}

        
        public List<SPEvolutionLogType> LogTypes { get; set; }

        public void Write(string message)
        {
            Write(message, SPEvolutionLogLevel.Debug);
        }
        public void Write(string message, SPEvolutionLogLevel level)
        {
            
            if (LogTypes != null)
            {
                foreach (SPEvolutionLogType log in LogTypes)
                {
                    switch (log)
                    {
                        case SPEvolutionLogType.Console:
                            Console.WriteLine(message);
                            break;
                        case SPEvolutionLogType.SharePointLog:
                            if (level == SPEvolutionLogLevel.Debug)
                            {
                                SPDiagnosticsService.Local.WriteTrace(0,
                                    new SPDiagnosticsCategory("SPEvolution", TraceSeverity.Monitorable, EventSeverity.Information),
                                        TraceSeverity.Monitorable, message);
                            }
                            else
                            {
                                SPDiagnosticsService.Local.WriteTrace(0,
                                    new SPDiagnosticsCategory("SPEvolution", TraceSeverity.Unexpected, EventSeverity.Error),
                                        TraceSeverity.Unexpected, message);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}
